"""2048核心算法(面向过程)
    list_merge = [2,0,2,0]
    -- 定义函数,对全局变量list_merge,
       实现零元素移动到末尾功能
       思想：从后向前判断,如果为零则删除,再末尾追加零
       2,0,2,0 --> 2,2,0,0
       2,0,4,2 --> 2,4,2,0
       2,0,0,2 --> 2 2 0 0"""

map = [
    [2, 0, 0, 2],
    [4, 2, 0, 2],
    [2, 4, 2, 4],
    [0, 4, 0, 4], ]


def del_0():
    for i in range(len(list_merge) - 1, -1, -1):
        if list_merge[i] == 0:
            del list_merge[i]
            list_merge.append(0)


# del_0()
# print(list_merge)
"""
-- 定义函数, 实现合并数字功能
思想：先调用第一个函数, 将零元素移动到末尾
如果是否相邻相同, 则后一个元素累加到前一个元素之上, 再删除后面元素, 最后末尾补零
2, 2, 4, 0 --> 4, 4, 0, 0
2, 2, 2, 0 --> 4, 2, 0, 0
2, 0, 2, 0 --> 2, 2, 0, 0 --> 4, 0, 0, 0
2, 0, 0, 2 --> 2, 2, 0, 0 --> 4, 0, 0, 0
2, 2, 0, 0 --> 4, 0, 0, 0"""


def move_left():
    for i in range(len(list_merge) - 1):
        for n in range(i + 1, len(list_merge)):
            if list_merge[i] == list_merge[n]:
                list_merge[i] += list_merge[n]
                del list_merge[n]
                list_merge.append(0)


# move_left()
# print(list_merge)
list_merge = []


def left_map_move():
    for i in map:
        global list_merge
        list_merge = i
        del_0()
        move_left()


def right_map_move():
    for i in map:
        global list_merge
        list_merge = i[::-1]
        del_0()
        move_left()
        i[::-1] = list_merge


# right_map_move()
# print(map)

"""--向上移动函数
    矩阵转置 --> 向左移动 --> 矩阵转置
map = [
    [2, 0, 0, 2],
    [4, 2, 0, 2],
    [2, 4, 2, 4],
    [0, 4, 0, 4], ]
--向下移动函数
    矩阵转置 --> 向右移动 --> 矩阵转置"""
new_map = []
def change_map():
    new_map = [list(i) for i in zip(*map)]
    map[:] = new_map
# for i in new_map:
#     print(i)

def up_move():
    global list_merge
    change_map()
    for i in map:
        list_merge = i
        del_0()
        move_left()
    change_map()



for i in map:
    print(i)
up_move()
print("*"*100)
for i in map:
    print(i)

def down_move():
    global list_merge
    for i in map:
        list_merge = i[::-1]
        del_0()
        move_left()
        i[::-1] = list_merge
        map[:] = new_map


# print(map)
# down_move()
# print(map)
